package com.aaa.controller;

import com.aaa.entity.T_Zf;
import com.aaa.service.T_ZfService;
import com.aaa.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: TT
 * @Date: 2019/3/30 14:44
 * @Version 1.0
 */
@Controller
@RequestMapping("/zf/")
public class T_ZfController {
    private List<T_Zf> zf = null;
    @Autowired
   private T_ZfService service;
    @RequestMapping("html")
    public String html() {
        return "statistics/zf";
    }
    @RequestMapping("select")
    @ResponseBody
    public Map selectScorce(@RequestBody Map map){
        List<Map> data=service.selectAll(map);
        Integer total=service.getCount(map);
        Map map1=new HashMap();
        map1.put("data",data);
        map1.put("total",total);
        return map1;
    }
        /**
         * 导出报表
         * @return
         */

        @RequestMapping("data")
        @ResponseBody
        public int excelKouFen(@RequestBody Map map) {
            zf = new ArrayList<T_Zf>();
            List<Map> list = (List) map.get("xinxi");
            //获取前端返回的xinxi 数组里面的数据
            for (int i = 0; i < list.size(); i++) {
                T_Zf zf1= new T_Zf();
                zf1.setName((String) list.get(i).get("name"));
                zf1.setAppscore((Integer) list.get(i).get("appscore"));
                zf1.setCpscore((Integer) list.get(i).get("cpscore"));
                zf1.setKscore((Integer) list.get(i).get("kscore"));
                zf1.setAverage1((Integer) list.get(i).get("average1"));
                zf1.setInscore((Integer) list.get(i).get("inscore"));
                zf1.setAa((Integer) list.get(i).get("aa"));
                zf1.setShtime((String) list.get(i).get("SHtime"));
                zf.add(zf1);
            }
            return 1;
        }
    @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数组数据
        List<T_Zf> list = zf;
        //定义excel标题
        String[] title = {"考试单位","社会评议","扣分","考评得分","知识检测","实地考察","总分","考核时间"};

        //excel文件名
        String fileName = "单位扣分表" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "单位扣分表";
        String[][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            T_Zf obj = list.get(i);
            content[i][0] = obj.getName();//1
            content[i][1] = String.valueOf(obj.getAppscore());//2
            content[i][2] = String.valueOf(obj.getCpscore());//3
            content[i][3] = String.valueOf(obj.getKscore());//4
            content[i][4] = String.valueOf(obj.getAverage1());//5
            content[i][5] = String.valueOf(obj.getInscore());//6
            content[i][6] = String.valueOf(obj.getAa());//7
            content[i][7] = obj.getShtime();//8
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
